package com.fd;

import lombok.Data;

/**
 * 慢查询日志
 *
 * @author YangXiaoLong
 * @since 1.0.0
 */
@Data
public class SlowQueryLog {
    /**
     * 查询时间
     */
    private String queryTime;
    /**
     * 用户
     */
    private String user;
    /**
     * 客户端ip
     */
    private String host;
    /**
     * 用户id
     */
    private String userId;
    /**
     * 本次查询花费时间
     */
    private Double queryTimeCost;
    /**
     * 键盘锁定时间
     */
    private Double lockTime;
    /**
     * 本次查询结果数据行
     */
    private Long rowsSent;
    /**
     * 查询扫描数据行
     */
    private Long rowsExamined;
    /**
     * sql
     */
    private String querySql;

    /**
     * 转化为mysql的插入语句
     *
     * @return {@link String}
     * @author YangXiaoLong
     * @since 1.0.0
     */
    public String toInsertSQL() {
        return String.format(
                "INSERT INTO slow_record (query_time, user, host, user_id, query_time_cost, lock_time, rows_sent, rows_examined, query_sql) " +
                        "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
                queryTime, user, host, userId, queryTimeCost, lockTime, rowsSent, rowsExamined, querySql.replaceAll("'", "''")
        );
    }
}
